package com.app.mobileshopapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.mobileshopapp.entity.Category;
import com.app.mobileshopapp.entity.Product;

import java.util.ArrayList;

public class ProductListActivity extends AppCompatActivity {
    ArrayList<Product> productList;
    GridView mGvProductView;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        initData();
        initView();
        initAdapter();
    }

    private void initAdapter() {
        adapter = new MyAdapter();
        mGvProductView.setAdapter(adapter);
    }

    private void initView() {
        mGvProductView = findViewById(R.id.gv_product);
    }

    private void initData() {
        productList = new ArrayList<>();
        Product product;
        product = new Product(1,
                1
                ,"荣耀路由Pro2"
                ,"rongyaopro2.jpg"
                ,"  荣耀路由Pro2 凌霄四核CPU 5G双频双千兆智能高速路由器 四信号大功率放大器 云存储 无线家用穿墙 IPv6"
                , 200.00f
                ,2000
        );
        productList.add(product);
        product = new Product(2,
                1
                ,"小米路由"
                ,"xiaomi.jpg"
                ,"小米（MI）路由器4A千兆版 双核CPU 全千兆 1200M双频无线速率 千兆端口 四天线穿墙 双频合一 光纤适用"
                , 299.00f
                ,2000
        );
        productList.add(product);
        product = new Product(3,
                1
                ,"360路由"
                ,"pro360.jpg"
                ,"360家庭防火墙5Pro 双千兆路由器 2100M无线家用 5G双频 F5pro 千兆版 千兆端口 光纤宽带WIFI穿墙"
                , 189.00f
                ,2000
        );
        productList.add(product);
        product = new Product(4,
                1
                ,"TPLink"
                ,"tplink.jpg"
                ,"TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器"
                , 180.00f
                ,2000
        );

        productList.add(product);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return productList.size();
        }

        @Override
        public Object getItem(int position) {
            return productList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View inflate = View.inflate(ProductListActivity.this, R.layout.product_list_item
                    , null);
            ImageView ivProductPic = inflate.findViewById(R.id.iv_product_pic);

            TextView tvProductName = inflate.findViewById(R.id.tv_product_name);
            TextView tvProductDesc = inflate.findViewById(R.id.tv_product_description);
            TextView tvProductPrice = inflate.findViewById(R.id.tv_product_price);

            final Product product = productList.get(position);
            ivProductPic.setImageResource(getImageIdentify(product));
            tvProductName.setText(product.getProductName());
            tvProductDesc.setText(product.getDescription());
            tvProductPrice.setText("价格:"+product.getPrice()+"元");

            inflate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ProductListActivity.this,ProductActivity.class);
                    intent.putExtra("product",product);
                    startActivity(intent);
                }
            });
            return inflate;
        }
    }
    private int getImageIdentify(Product product) {
        int identifier = getResources()
                .getIdentifier(product.getProductPic().split("\\.")[0]
                        , "mipmap", getPackageName());
        return identifier;
    }
}
